﻿/*************************************************************************************

- CLR版本：        4.0.30319.42000
- 类 名 称：       KeyenceTCP
- 机器名称：       YTE
- 命名空间：       LD.Common.PLC
- 文 件 名：       KeyenceTCP
- 创建时间：       2022/5/9 10:11:07
- 作    者：       YTE
- 说   明：
- 修改时间：
- 修 改 人：
  *************************************************************************************/

using LD.Host;
using LdCommunication;
using LdCommunication.Core;
using LdCommunication.Profinet.Keyence;
using System;
using System.Threading.Tasks;

namespace LD.PLC
{
    public class KeyenceTCP : PLCAbstract
    {
        private TaskEx taskEx = new TaskEx();

        private KeyenceNanoSerialOverTcp keyTcp;
        public KeyenceTCP()
        {
            keyTcp = new KeyenceNanoSerialOverTcp();
            IByteTransform ByteTransform = new RegularByteTransform();
            ByteTransform.IsStringReverseByteWord = true;
            keyTcp.ByteTransform = ByteTransform;
        }
        private Status status = Status.失败;

        public override event ConnectionStatusDelegate StatusEvent;

        public override Status Status
        {
            get { return status; }
            set
            {
                status = value;
                StatusEvent?.BeginInvoke(Name, value, null, keyTcp);
            }
        }
        public override OperateResult<byte[]> Read(string address, ushort val)
        {
            return keyTcp.Read(address, val);
        }

        public override OperateResult<bool[]> ReadBool(string address, ushort val)
        {
            return keyTcp.ReadBool(address, val);
        }

        public override OperateResult<bool> ReadBool(string address)
        {
            return keyTcp.ReadBool(address);
        }

        public override OperateResult<int> ReadInt(string address)
        {
            return keyTcp.ReadInt32(address);
        }

        public override OperateResult<int[]> ReadInt(string address, ushort val)
        {
            return keyTcp.ReadInt32(address, val);
        }

        public override OperateResult<short> ReadInt16(string address)
        {
            return keyTcp.ReadInt16(address);
        }

        public override OperateResult<short[]> ReadInt16(string address, ushort val)
        {
            return keyTcp.ReadInt16(address, val);
        }

        public override OperateResult<string> ReadString(string address, ushort val)
        {
            return keyTcp.ReadString(address, val);
        }

        public override void Start()
        {
            if (HDParam != null)
            {
                keyTcp.IpAddress = HDParam.Address;
                keyTcp.Port = HDParam.Port;
                keyTcp.ConnectTimeOut = HDParam.TimeOut;
                keyTcp.ReceiveTimeOut = HDParam.TimeOut;
                taskEx.Start(Connection, null);
            }
        }

        public override void Stop()
        {
            taskEx?.Cancel();
            keyTcp?.ConnectClose();
        }
        private void Connection(object param)
        {
            try
            {
                if (status == Status.失败)
                {
                    OperateResult operate = keyTcp.ConnectServer();
                    if (operate.IsSuccess)
                        Status = Status.成功;
                    else
                        Status = Status.失败;
                }
            }
            catch (Exception ex)
            {
                Status = Status.失败;
                Log4.Error(ex);
            }
            Task.Delay(3000).Wait();
        }
        public override OperateResult Write(string address, short val)
        {
            return keyTcp.Write(address, val);
        }

        public override OperateResult Write(string address, int val)
        {
            return keyTcp.Write(address, val);
        }

        public override OperateResult Write(string address, bool val)
        {
            return keyTcp.Write(address, val);
        }

        public override OperateResult Write(string address, string val)
        {
            return keyTcp.Write(address, val);
        }

        public override OperateResult Write(string address, byte[] val)
        {
            return keyTcp.Write(address, val);
        }

        public override OperateResult<float> ReadFloat(string address)
        {
            return keyTcp.ReadFloat(address);
        }

        public override OperateResult<float[]> ReadFloat(string address, ushort val)
        {
            return keyTcp.ReadFloat(address, val);
        }

        public override OperateResult Write(string address, float val)
        {
            return keyTcp.Write(address, val);
        }

        public override bool Wait(string address, short val, int timeout)
        {
            bool result = false;
            var time = keyTcp.Wait(address, val, 200, timeout);
            if (time.IsSuccess)
            {
                result = true;
            }
            return result;
        }

        public override bool Wait(string address, bool val, int timeout)
        {
            bool result = false;
            var time = keyTcp.Wait(address, val, 200, timeout);
            if (time.IsSuccess)
            {
                result = true;
            }
            return result;
        }

        public override OperateResult Write(string address, double val)
        {
            return keyTcp.Write(address, val);
        }
    }
}
